package cn.kgc.ssm.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.ssm.entity.Member;
import cn.kgc.ssm.mapper.MemberMapper;
import cn.kgc.ssm.service.MemberService;
import cn.kgc.ssm.utils.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/3/8
 * @description
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper memberMapper;


    @Override
    public ResponseResult register(Member member) {

        ResponseResult responseResult = new ResponseResult();
        // 判定会员名是否被占用
        Member member1 =   memberMapper.selectByName(member.getMemberName());

        if (member1!=null){
            responseResult.setCode(110);
            responseResult.setMsg("会员名已被注册");
            return responseResult;
        }


        //密码加密
        member.setPassword(SecureUtil.md5(member.getPassword()));
        memberMapper.registerMember(member);


        responseResult.setCode(0);
        responseResult.setMsg("注册成功");
        return responseResult;
    }

    @Override
    public ResponseResult login(String memberName, String password, HttpSession session) {

        ResponseResult responseResult = new ResponseResult();

        Member member =  memberMapper.selectByNameAndPassword(memberName,SecureUtil.md5(password));

        if (member==null){
            responseResult.setCode(110);
            responseResult.setMsg("用户名或密码错误");
            return  responseResult;
        }

        // 缓存会员信息
        session.setAttribute("memberInfo",member);

        responseResult.setCode(0);
        responseResult.setMsg("登录成功");

        return responseResult;
    }

    @Override
    public ResponseResult memberPage(Integer page, Integer limit, String memberName) {

        PageHelper.startPage(page,limit);

         List<Member> members =  memberMapper.selectList(memberName);
        PageInfo<Member> memberPageInfo = new PageInfo<>(members);
        long total = memberPageInfo.getTotal();


        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("查询成功");
        responseResult.setCount(total);
        responseResult.setData(members);
        return responseResult;
    }

    @Override
    public ResponseResult getMemberByName(String name) {

        Member member = memberMapper.selectByName(name);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setData(member);
        return responseResult;
    }

    @Override
    public ResponseResult addMember(Member member) {

        ResponseResult  responseResult = new ResponseResult();
        //  校验会员名是否重复
        Member member1 = memberMapper.selectByName(member.getMemberName());
        if (member1!=null){
            responseResult.setCode(110);
            responseResult.setMsg("会员名已存在");
            return  responseResult;
        }

        // 设置默认密码
        member.setPassword(SecureUtil.md5("666666"));
        memberMapper.addMember(member);
        responseResult.setCode(0);
        responseResult.setMsg("添加成功");

        return responseResult;
    }

    @Override
    public ResponseResult editMember(Member member) {

        memberMapper.editMember(member);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("更新成功");
        return responseResult;
    }

    @Override
    public ResponseResult deleteMember(Integer[] ids) {

        memberMapper.deleteMember(ids);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("删除成功");
        return responseResult;
    }

    @Override
    public ResponseResult resetPwd(Integer id) {

        String password = SecureUtil.md5("666666");
        memberMapper.resetPwd(id,password);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("密码重置成功");
        return responseResult;
    }

}
